#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void swap(int* as, int* aq)
{
	int tmp = *as;
	*as = *aq;
	*aq = tmp;
}
void adjustdown(int* a, int n,int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] > a[child])
		{
			child++;
		}
		if (a[parent] < a[child])
		{
			swap(&a[parent], &a[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
void duipaixu(int* a, int n)
{
	for (int i = (n - 1 - 1) / 2; i>=0; i--)
	{
		adjustdown(a, n, i);
	}
	int end = n - 1;
	while (end>0)
	{
		swap(&a[0], &a[end]);
		adjustdown(a, end, 0);
		end--;
	}
}
int main()
{
	int arr[] = { 43,56,6578,78903,53,14,4678,92,5,567812,45,67,124,47 };
	duipaixu(arr, sizeof(arr) / sizeof(0));
	for (int i = 0; i < sizeof(arr) / sizeof(0); i++)
	{
		printf("%d ", arr[i]);
	}

	return 0;
}